//
//  BHCompetitionFilterView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/10.
//

import UIKit
@objc protocol BHCompetitionFilterViewDelegate: NSObjectProtocol {
    func filterHeaderDidChose(sender: UIButton)
    func fitterHeaderBegainSearch()
    func filterHeaderSearchForKey(key: String)
}

class BHCompetitionFilterView: UIView {
    
    var delegate:BHCompetitionFilterViewDelegate?
    
    
    @IBOutlet weak var searchTextFiled: UITextField!
    @IBOutlet weak var search_backView: UIView!
    
    @IBOutlet var filterBtnArray: [UIButton]!
    
    fileprivate var remarkBtn: UIButton?
    
    var hideItemIndex: Int = -1 {
        didSet {
            for (index,btn) in self.filterBtnArray.enumerated() {
                if hideItemIndex == index {
                    btn.isHidden = true
                }else {
                    btn.isHidden = false
                }
            }
        }
    }
    

    static func loadFromXib() -> BHCompetitionFilterView {
        let viewArray = Bundle.main.loadNibNamed("BHCompetitionFilterView", owner: nil, options: nil)
        let view: BHCompetitionFilterView = viewArray![0] as! BHCompetitionFilterView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.search_backView.addLayerCorner(16, 0, .white)
        
        self.searchTextFiled.delegate = self
    }
    
    static var headerHeight: CGFloat {
        get {
            let searchHeight = CGFloat(32).radius
            return (searchHeight+20+45)
        }
    }
    
    
    func changeIndexBtnText(index: Int,text: String)  {
        if index < self.filterBtnArray.count {
            let btn = self.filterBtnArray[index]
            btn.setTitle(text, for: .normal)
            btn.setTitle(text, for: .selected)
        }
    }
    
    
    
    @IBAction func filterBtnAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        sender.isSelected = true
        
        self.remarkBtn?.isSelected = false
        
        self.delegate?.filterHeaderDidChose(sender: sender)
        
        self.remarkBtn = sender
    }
    
    
    func resetAllState()  {
        for btn in self.filterBtnArray {
            btn.isSelected = false
        }
        self.remarkBtn = nil
    }
}

extension BHCompetitionFilterView: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.delegate?.fitterHeaderBegainSearch()
        return true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.delegate?.filterHeaderSearchForKey(key: textField.text ?? "")
    }
}



class BHCompetitionAreaLocationView: UIView {
    static func loadFromXib() -> BHCompetitionAreaLocationView {
        let viewArray = Bundle.main.loadNibNamed("BHCompetitionFilterView", owner: nil, options: nil)
        let view: BHCompetitionAreaLocationView = viewArray![1] as! BHCompetitionAreaLocationView
        return view
    }
    
    static var viewHeight: CGFloat {
        get {
            return 40
        }
    }
}
